import java.rmi.RemoteException;
import java.util.*;
import javax.microedition.io.*;
import java.io.*;


public class Utils
{
    public static String extractTimeString(Date time)
    {
        // Todo:
        // make this a bit more robust.
        
        return time.toString().substring(10,19).trim();
    }
    
    public static String uploadResults(String results)
    {
        String returnedResults = "";
        try {
             returnedResults = postResults("http://localhost:53451/Website/Upload.aspx", results);  
        } catch (IOException e) {
           int i=0;
        }
        return returnedResults;
        
    }
    
//  private static String sendHttpPost( String url,String requeststring )
//    {
//        HttpConnection      hcon = null;
//        DataInputStream     dis = null;
//        DataOutputStream    dos = null;
//        StringBuffer        responseMessage = new StringBuffer();
//        
//        try 
//        {
//            hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );
//            
//            hcon.setRequestMethod( HttpConnection.POST );
//           // hcon.setRequestProperty("enctype", "multipart/form-data" );
//            
//            dos = hcon.openDataOutputStream();
//            byte[] request_body = requeststring.getBytes();
//            
//            for( int i = 0; i < request_body.length; i++ ) {
//                dos.writeByte( request_body[i] );
//            }
//
//            dis = new DataInputStream( hcon.openInputStream() );
//            
////            int ch;
////            while( ( ch = dis.read() ) != -1 ) {
////                responseMessage.append( (char)ch );
////            }
//        }
//        catch( Exception e )
//        {
//            e.printStackTrace();
//            responseMessage.append( "ERROR" );
//        } 
//        finally {
//            // free up i/o streams and http connection
//            try {
//                if( hcon != null ) hcon.close();
//                if( dis != null ) dis.close();
//                if( dos != null ) dos.close();
//            } catch ( IOException ioe ) {
//                ioe.printStackTrace();
//            }
//        }
//        return responseMessage.toString();
//    }

  
      private static String postResults(String url, String requeststring) throws IOException {
        HttpConnection connection = null;
        DataInputStream is = null;
        DataOutputStream os = null;
        StringBuffer returnedResultsBuffer = new StringBuffer();
        
        try {
          connection = (HttpConnection)Connector.open(url,Connector.READ_WRITE );
          connection.setRequestMethod(HttpConnection.POST);
          os = connection.openDataOutputStream();
         
          byte postmsg[] = requeststring.getBytes();
          //os.write(postmsg);
          
          for(int i=0;i<postmsg.length;i++) {
            os.writeByte(postmsg[i]);
          }
          os.flush();
          int rc = connection.getResponseCode();
          String rm = connection.getResponseMessage();

          is = connection.openDataInputStream();
          int ch;
          while ((ch = is.read()) != -1) {
             char c = (char) ch;
            returnedResultsBuffer.append(c);
          }
        } finally {
           if(is!= null) {
              is.close();
           }
           if(os != null) {
              os.close();
           }
           if(connection != null) {
              connection.close();
           }
        }
        
        return returnedResultsBuffer.toString();
    }     
}
